package com.baibei.module;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 日期工具
 * Created by ChenRui on 2017/8/4 0004 15:24.
 */
public final class DateUtils {

    private static final DecimalFormat decimalFormat = new DecimalFormat();

    /**
     * 计算预计到港时间
     */
    public static String getLessTime(Long time) {
        if (time == null) {
            return "已过期";
        }
        long span = time - System.currentTimeMillis();
        if (span <= 0) {
            return "已过期";
        } else if (span > 86400000) {
            return String.format(Locale.getDefault(), "%1d天", (span / 86400000));
        } else if (span > 3600000) {
            return String.format(Locale.getDefault(), "%d小时", (span / 3600000));
        } else if (span > 60000) {
            return String.format(Locale.getDefault(), "%d分", (span / 60000));
        } else {
            return String.format(Locale.getDefault(), "%d秒", (span / 1000));
        }
    }


    @SuppressWarnings("SimpleDateFormat")
    public static String formatDate(long time) {
        return formatDate(time, "yyyy-MM-dd HH:mm:ss");
    }

    @SuppressWarnings("SimpleDateFormat")
    public static String formatDate(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }
}
